package com.example.project.Function.user;

import static com.example.project.key.connection.DBUtils.getConnection;
import static com.example.project.key.connection.DBUtils.getinfo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.project.Function.listview.employee_alarm_Adapter;
import com.example.project.R;
import com.example.project.key.connection.databaseconn;
import com.example.project.key.object.mission;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class alarm extends Fragment {
    private ListView l1;
    private Button btn_search;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle saveInstanceState) {
        View view = inflater.inflate(R.layout.alarm, container, false);

        return view;
    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        btn_search=getActivity().findViewById(R.id.btn_s);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                l1 = getActivity().findViewById(R.id.alarm_list);
                final Handler handler = new Handler()
                {
                    @SuppressLint("HandlerLeak")
                    public void handleMessage(@NonNull Message msg)
                    {
                        super.handleMessage(msg);

                        if(msg.what==1)
                        {
                            List<HashMap<String,Object>> list = (List<HashMap<String, Object>>) msg.obj;
                            SimpleAdapter simpleAdapter=new SimpleAdapter(getContext(),list,R.layout.mission_item,new String[]{"mission","company","mt","number"},new int[]{R.id.mission_what,R.id.order_company,R.id.mission_time,R.id.order_num});
//                            为listview设置适配器
                            l1.setAdapter(simpleAdapter);
                        }
                    }
                };

                Thread thread=new Thread(new Runnable() {
                    //                    定义一个子线程中的全局变量List<HashMap<String,Object>> list1，用于接收从DBUtils中返回的list
                    List<HashMap<String,Object>> list1=new ArrayList<HashMap<String, Object>>();
                    @Override
                    public void run() {
//                        与数据库建立连接
                        getConnection("android");
                        try {
//                            以下这些要用try/catch包含
//                            调用数据库工具类的getinfo函数，用list1接收返回的list数据
                            list1=getinfo("android");

//                            打印日志，测试用
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
//                将从数据库拿到的list1对象传给message再由handler传出，再在handler中处理，可进行更新UI
//                        新建一个message对象，尽量不要直接new，而是用这种方法，因为有内存的问题存在
                        Message message=Message.obtain();

//                        设置message的辨认码，这里设为1
                        message.what=1;

//                        把刚才接收到的list1赋给message.obj对象
                        message.obj=list1;

//                        通过handler将携带数据的message传出去，传到handler中
                        handler.sendMessage(message);
                    }
                });

//                上面线程定义完了，现在启动线程
                thread.start();

            }
        });
    }
}
